<?php
class class_page implements ArrayAccess
{
	var $currentPage;	//1起步
	var $pageSize;
	var $count = null;
	var $pageCount = null;
	
	var $firstPage;
	var $lastPage;
	var $prevPage;
	var $nextPage;
	
	var $hasNext;
	var $hasPrev;
	
  function __construct($count, $currentPage, $pageSize = 20)
  {  	
      $this->pageSize = $pageSize ? $pageSize : 20;
      $this->count = (int)$count;
      $this->pageCount = ceil($this->count / $this->pageSize);
      
      $oldPage = $currentPage;
      
      if($currentPage != -1)
      {
        $currentPage = (int)$currentPage;
        
        if($currentPage > $this->pageCount)
        	$currentPage = $this->pageCount;
        else if($currentPage < 1)
        	$currentPage = 1;
        
        $this->init($currentPage);
      }
      
      $this->hasNext = ($this->currentPage+1) <= $this->lastPage;
      $this->hasPrev = $this->currentPage != 1;
  }
//-------------------------------------------------------------------------------
	function setMaxCount($value)
	{
		if($this->count > $value)
		{
			$this->count = $value;
			$this->pageCount = ceil($value / $this->pageSize);
		}
	}
//-------------------------------------------------------------------------------
	function init($currentPage = 1)
	{
      $this->currentPage = $currentPage ? $currentPage : 1;
      $this->firstPage = 1;
      $this->lastPage = $this->pageCount > $this->firstPage ? $this->pageCount : 1;

      if ($this->currentPage >= $this->lastPage) {
          $this->nextPage = $this->lastPage;
      } else {
          $this->nextPage = $this->currentPage+1;
      }

      if ($this->currentPage <= 1) {
          $this->prevPage = 1;
      } else {
          $this->prevPage = $this->currentPage-1;
      }
	}
//-------------------------------------------------------------------------------
	function isLast()
	{
		return $this->currentPage >= $this->lastPage;
	}
//-------------------------------------------------------------------------------
	function getLimit()
	{
		return array($this->getOffset(), $this->pageSize);
	}
//-------------------------------------------------------------------------------
  function getOffset()
  {
  	return $this->pageSize * ($this->currentPage - 1);
  }
//-------------------------------------------------------------------------------
	function getRange()
	{
		if($this->lastPage - $this->firstPage > 100)
		{
			$array = range($this->firstPage, 50);
			$array = array_join($array, range($this->firstPage - 50, $this->lastPage));
		}
		else
		{
			$array = range($this->firstPage, $this->lastPage);
		}
		
		return array_samkv($array);
	}
//-------------------------------------------------------------------------------
	function url($url, $type, $key = 'page')
	{
		if($type == 'f')
			$num = $this->firstPage;
		else if($type == 'l')
			$num = $this->lastPage;
		else if($type == '<')
			$num = $this->prevPage;
		else if($type == '>')
			$num = $this->nextPage;
		else if(is_numeric($type))
			$num = $type;
		
		$char = strpos($url, '?') === false ? '?' : '&';
		
		return "{$url}{$char}{$key}={$num}";
	}
//-------------------------------------------------------------------------------
	function showPage($current = 0, $num = 4)
	{
		if(!$current)
			$current = $this->currentPage;
		
		$min = $current - ceil($num / 2);		
		$min = $this->firstPage > $min ? $this->firstPage : $min;
		
		$max = $min + $num - 1;
		
		if($this->lastPage < $max)
		{
			$max = $this->lastPage;
			$min = max($max - $num, 1);
		}
		
		return range($min, $max);
	}
//-------------------------------------------------------------------------------
	function offsetExists($index)
	{
		return isset($this->{$index});
	}
//-------------------------------------------------------------------------------
	function offsetGet($index)
	{
		return $this->{$index};
	}
//-------------------------------------------------------------------------------
	function offsetSet($index, $newvalue)
	{
		$this->{$index} = $newvalue;
	}
//-------------------------------------------------------------------------------
	function offsetUnset($index)
	{
		unset($this->{$index});
	}
}
